<script setup lang="ts">
import { requiredCheck, singleIpCheck, validatePortCheck } from "@/utils/verify";
import { warnAction, warnAdd, warnListSip } from "@/api/modules/threat/index";
import DateRange from "@/components/DateRange/index.vue";

interface Props {
  title?: string;
  rowData?: Object;
  type?: number;
}

const props = withDefaults(defineProps<Props>(), {
  title: "详情",
  rowData: undefined,
  type: 1 // 1=封堵 2=删除 3=人工封堵
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";
import SelectLinksInput from "@/components/SelectLinksInput/index.vue";
import BaseButton from "@/components/BaseButton/index.vue";
import UpdateForm from "@/views/system/account/components/UpdateForm/index.vue";
import UpdateFormPassword from "@/views/system/account/components/UpdateFormPassword/index.vue";
import useSearch from "@/hooks/useSearch";
import { getDataByPage } from "@/api/modules/system/account";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {
    sip: [...requiredCheck("请输入IP"), { validator: singleIpCheck }]
  },
  defaultData: {
    plug_class: "1"
  }
});

async function submitCb() {
  const sip = updateFormData.sip;
  await submit({
    addApi: warnAction,
    updateApi: warnAction,
    sendData: {
      sip: updateFormData.sip,
      action: props.type == 2 ? 4 : 1,
      desc: "",
      plug_class: updateFormData.plug_class
    }
  });
  if (props.type == 3) {
    await warnAdd({
      vlanid: 0,
      sip
    });
  }

  emits("success");
}

function openHandleCb() {
  openHandle({
    row: {
      ...props.rowData
    },
    id: props.rowData?.id
  });
  if (props.rowData?.sip) {
    formData.sip = props.rowData?.sip;
  }
}

import useData from "./hooks/useData";
import { useUserStore } from "@/stores/modules/user";
import SelectThreatInput from "@/components/SelectThreatInput/index.vue";

const { pageData, roleMap } = useData();

const { tableData, loadData, formData, reloadData } = useSearch({
  defaultData: {},
  responseKey: {
    list: "result.data",
    total: "result.total"
  }
});

function loadDataCb() {
  loadData({
    api: warnListSip,
    sendData: {
      pageNum: formData.pageNum - 1,
      pageSize: formData.pageSize,
      sip: props.rowData?.sip,
      threatId: formData?.threatId,
      start_time: formData.start_time ? parseInt(new Date(formData.start_time).getTime() / 1000) : "",
      end_time: formData.end_time ? parseInt(new Date(formData.end_time).getTime() / 1000) : ""
    }
  });
}

loadDataCb();

function reloadDataCb() {
  reloadData();
  loadData({
    api: warnListSip,
    sendData: {
      pageNum: formData.pageNum - 1,
      pageSize: formData.pageSize,
      sip: props.rowData?.sip,
    }
  });
}

loadDataCb();
</script>

<template>
  <div class="update-form mg-r-sm" style="display: inline-block" @click="openHandleCb">
    <slot>
      <el-button type="primary">新增</el-button>
    </slot>
  </div>
  <base-dialog @confirm="submitCb()" width="850px" :title="title" v-model="showHandle">
    <div class="details">
      <el-form class="h-box" style="margin-left: 40px">
        <el-form-item label="攻击类型">
          <SelectThreatInput width="140px" v-model="formData.threatId"></SelectThreatInput>
        </el-form-item>
        <el-form-item label="时间" class="mg-l-lg">
          <DateRange width="240" v-model:start="formData.start_time" v-model:end="formData.end_time"></DateRange>
          <base-button @click="loadDataCb" class="mg-l-sm" type="search_icon_text"></base-button>
          <base-button @click="reloadDataCb" type="refresh_icon_text"></base-button>
        </el-form-item>
      </el-form>
      <div class="details-main" style="width: 90%; margin: auto">
        <he-table
          @handle-current-change="loadDataCb"
          :total="tableData.total"
          v-model:page-size="formData.pageSize"
          v-model:page-num="formData.pageNum"
          operate-width="180px"
          :operate-list="[]"
          :table-data="tableData.list"
          :column-data="pageData.columnData"
          :show-multiple="false"
          :show-summary="false"
          :show-operate="false"
          :show-multiple-column="false"
          :show-pagination="true"
        >
        </he-table>
      </div>
    </div>
    <template #footer-more-button></template>
  </base-dialog>
</template>

<style lang="scss" scoped></style>
